package com.apache.client;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.apache.api.vo.ResultEntity;
import com.apache.tools.StrUtil;

public class DictSelectServlet extends HttpServlet {

	private static final long serialVersionUID = 3145869821392333253L;
	private Logger logger = Logger.getLogger(getClass().getName());

	/**
	 * Constructor of the object.
	 */
	public DictSelectServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String doCode = request.getParameter("doCode");
		if (StrUtil.isNull(doCode)) {
			defaultMethod(request, response);
			return;
		}
		try {
			Method method = DictSelectServlet.class.getDeclaredMethod(doCode, new Class[] { HttpServletRequest.class,
					HttpServletResponse.class });
			method.invoke(this, request, response);
		} catch (Exception e) {
			logger.error(e);
			JSONObject json = new JSONObject();
			json.put("flag", "F");
			json.put("msg", "系统执行异常,请稍后重试");
			outputJson(json.toString(), response);
		}
	}

	public void defaultMethod(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		String cateEname = request.getParameter("cateEname");
		String fatherValue = request.getParameter("fatherValue");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("cateEname", cateEname);
		dataMap.put("fatherValue", fatherValue);
		this.logger.info("cateEname=" + cateEname + ",fatherValue=" + fatherValue);

		ResultEntity root = DictCoreClient.getInstance().dictSelect(dataMap);

		if (null == root) {
			this.logger.info("root=" + root);
		} else {
			this.logger.info("result=" + root.getResult() + ",message=" + root.getMessage());
		}
		outputJson(String.valueOf(root.getEntity()), response);
	}

	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cateEname = request.getParameter("cateEname");
		String fatherValue = StrUtil.doNull(request.getParameter("fatherValue"), "0");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("cateEname", cateEname);
		dataMap.put("fatherValue", fatherValue);
		if (StrUtil.isNull(request.getParameter("page"))) {
			dataMap.put("pageIndex", request.getParameter("page"));
		}
		if (StrUtil.isNull(request.getParameter("pageSize"))) {
			dataMap.put("pageSize", request.getParameter("pageSize"));
		}
		this.logger.info("cateEname=" + cateEname + ",fatherValue=" + fatherValue);
		ResultEntity root = DictCoreClient.getInstance().dictList(dataMap);
		if (null == root) {
			this.logger.info("root=" + root);
		} else {
			this.logger.info("result=" + root.getResult() + ",message=" + root.getMessage());
		}
		outputJson(String.valueOf(root.getEntity()), response);
	}

	public void doCity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cateEname = request.getParameter("cateEname");
		String fatherValue = request.getParameter("fatherValue");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("cateEname", cateEname);
		dataMap.put("fatherValue", fatherValue);
		this.logger.info("cateEname=" + cateEname + ",fatherValue=" + fatherValue);
		Map result = DictCoreClient.getInstance().dictCityInfo(dataMap);
		if (null == result) {
			JSONObject array = JSONObject.fromObject("{}");
			outputJson(array.toString(), response);
		} else {
			JSONObject array = JSONObject.fromObject(result);
			outputJson(array.toString(), response);
		}
	}

	public void doView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cateEname = request.getParameter("cateEname");
		String itemValue = request.getParameter("itemValue");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("cateEname", cateEname);
		dataMap.put("itemValue", itemValue);
		this.logger.info("cateEname=" + cateEname + ",itemValue=" + itemValue);
		Object result = DictCoreClient.getInstance().dictItemInfo(dataMap);
		if (null == result) {
			JSONObject array = JSONObject.fromObject("{}");
			outputJson(array.toString(), response);
		} else {
			JSONObject array = JSONObject.fromObject(result);
			outputJson(array.toString(), response);
		}
	}

	/**
	 * 输出json数据
	 */
	protected void outputJson(String json, HttpServletResponse response) throws IOException {
		if (StrUtil.isNotNull(json)) {
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		}
	}

	public void init() throws ServletException {
	}

}
